package com.wuwei.elearning.grade;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.elearning.question.domain.QuestionItem;
import com.wuwei.elearning.question.domain.TestResult;
import com.wuwei.elearning.question.mapper.QuestionItemMapper;
import com.wuwei.elearning.question.mapper.TestResultMapper;
import com.wuwei.elearning.setting.domain.User;
import com.wuwei.elearning.setting.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@SpringBootTest
public class gradeTest1 {


    @Resource
    private UserMapper userMapper;

    @Resource
    private TestResultMapper testResultMapper;

    @Resource
    private QuestionItemMapper questionItemMapper;


    /**
     * 查询某名学生在该班级平均分的排名
     * 1.查询该学生所在班级的所有其他学员
     * 2.查询该名学员和所有其他学员的平均分
     */
    @Test
    public void test1(){
        Integer gradeId = 12;
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getGradeId,gradeId);
        //1.查询该学生所在班级的所有其他学员
        List<User> users = userMapper.selectList(queryWrapper);
        //查询所有学员的平均分
        List<Long> accounts = users.stream().map(User::getAccount).collect(Collectors.toList());
        //先查询该班级的所有试卷
        LambdaQueryWrapper<QuestionItem> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(QuestionItem::getGradeId,gradeId);
        List<QuestionItem> questionItems = questionItemMapper.selectList(queryWrapper1);
        int count = questionItems.size();
        Map<Long,Float> map = new HashMap<>();
        //再根据accounts查询各位学员在这些试卷的成绩，如果查不出成绩就代表成绩为0
        for (Long account : accounts) {
            float allScore = 0;
            for (QuestionItem questionItem : questionItems) {
                LambdaQueryWrapper<TestResult> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(TestResult::getQuestionItemId,questionItem.getId()).eq(TestResult::getAccount,account);
                TestResult testResult = testResultMapper.selectOne(queryWrapper2);
                float score = testResult==null?0:testResult.getScore();
                allScore+=score;
            }
            //算出平均分
            float average = allScore/count;
            map.put(account,average);
        }


    }

    /**
     * 根据 key 查找 value 的排名
     * @param targetKey
     * @param scores Map<Long,Float>这样的数据，key表示account，学员账号，value表示学员的平均分
     * @return
     */
    private int getRank(Long targetKey, Map<Long, Float> scores) {

        // 获取对应的 value
        Float targetValue = scores.get(targetKey);
        if (targetValue == null) {
            System.out.println("Key not found.");
            return -1;
        }

        // 获取所有的 value 并进行排序
        List<Float> values = new ArrayList<>(scores.values());
        Collections.sort(values, Collections.reverseOrder()); // 降序排序

        // 计算目标 value 的排名
        int rank = 1; // 排名从 1 开始
        for (Float value : values) {
            if (value.equals(targetValue)) {
                break;
            }
            rank++;
        }

        System.out.println("The rank of the value for key " + targetKey + " is: " + rank);

        return rank;
    }
}
